package com.newland;

import com.newland.util.DBUtils;
import com.newland.util.MyUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Properties;

/**
 *Description:
 *Version:1.0.0
 *@author shihb
 *@date 2020/3/30 15:40
 */
public class Oracle2File {

  public static void main(String[] args){
    if(args.length>0){
      MyUtils.loadConfigFile(args[0]);
    }else {
      MyUtils.loadConfigFile("config.properties");
    }
    PrintWriter pw = null;


    try {
      Properties prop= MyUtils.getConfig();
      File resultFile=new File(prop.getProperty("resultFile"));
      if(resultFile.exists()){
        resultFile.delete();
      }
      // 时间处理
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      String sqlDate = sdf.format(System.currentTimeMillis());
      String year = sqlDate.substring(0,4);

      pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(resultFile), "UTF-8"));

      // 获取最新年累计赋码数
//      String countTarceSql = "SELECT COUNT(*) FROM  ENT_BATCH_TRACE_CODE_INFO WHERE TO_CHAR(CREATE_TIME,'YYYY')="+year;
      String countTarceSql = "SELECT COUNT(*) FROM MD_BS_TRACE_CODE_LIST";
      ResultSet countTarceRs = DBUtils.executeSql(countTarceSql);
      while (countTarceRs.next()){
        StringBuilder sb = new StringBuilder();
        sb.append("insert into MD_BS_TRACE_CODE_SUM(STAT_DATE,TRACE_CODE_CNT,IMP_TIME) values(");
        sb.append("'").append(year).append("',");
        sb.append("'").append(countTarceRs.getString(1)).append("',");
        sb.append("to_date('").append(sqlDate).append("','yyyy-mm-dd hh24:mi:ss')");
        sb.append(")");
        pw.println(sb.toString());
      }
      // 获取最新年累计扫码数
      String countSearchSql = "SELECT COUNT(*) FROM  TRACE_CODE_SEARCH_LOG WHERE TO_CHAR(SEARCH_TIME,'YYYY')="+year;
//      String countSearchSql = "SELECT COUNT(*) FROM MD_BS_TRACE_CODE_LIST" ;
      ResultSet countSearchRs = DBUtils.executeSql(countSearchSql);
      while (countSearchRs.next()){
        StringBuilder sb = new StringBuilder();
        sb.append("insert into MD_BS_SEARCH_LOG_SUM(STAT_DATE,TRACE_CODE_CNT,IMP_TIME) values(");
        sb.append("'").append(year).append("',");
        sb.append("'").append(countSearchRs.getString(1)).append("',");
        sb.append("to_date('").append(sqlDate).append("','yyyy-mm-dd hh24:mi:ss')");
        sb.append(")");
        pw.println(sb.toString());
      }
      // 获取最新年累计赋码明细
      String sql = "SELECT * FROM  MD_BS_TRACE_CODE_LIST  where rownum<="+prop.getProperty("getTraceCodeNum")+"order by CREATE_TIME";

      // 执行sql获取赋码明细
      ResultSet rs = DBUtils.executeSql(sql);
      //判断是否有值
      while (rs.next()){
        StringBuilder sb = new StringBuilder();
        sb.append("insert into MD_BS_TRACE_CODE_LIST values(");
        sb.append("'").append(rs.getString(1)).append("',");
        sb.append("'").append(rs.getString(2)).append("',");
        sb.append("'").append(rs.getString(3)).append("',");
        sb.append("'").append(rs.getString(4)).append("',");
        sb.append("'").append(rs.getString(5)).append("',");
        sb.append("'").append(rs.getString(6)).append("',");
        sb.append("'").append(rs.getString(7)).append("',");
        sb.append("'").append(rs.getString(8)).append("',");
        sb.append("'").append(rs.getString(9)).append("',");
        sb.append("'").append(rs.getString( 10)).append("',");
        sb.append("'").append(rs.getString(11)).append("',");
        sb.append("'").append(rs.getString(12)).append("',");
        sb.append("'").append(rs.getString(13)).append("',");
        sb.append("'").append(rs.getString(14)).append("',");
        sb.append("'").append(rs.getString(15)).append("',");
        sb.append("'").append(rs.getString(16)).append("',");
        sb.append("'").append(rs.getString(17)).append("',");
        sb.append("'").append(rs.getString(18)).append("',");
        sb.append("'").append(rs.getString(19)).append("',");
        sb.append("'").append(rs.getString(20)).append("',");
        sb.append("'").append(rs.getString(21)).append("',");
        sb.append("'").append(rs.getString(22)).append("',");
        sb.append("'").append(rs.getString(23)).append("',");
        sb.append("'").append(rs.getString(24)).append("',");
        sb.append("'").append(rs.getString(25)).append("',");
        sb.append("'").append(rs.getString(26)).append("',");
        sb.append("'").append(rs.getString(27)).append("',");
        sb.append("'").append(rs.getString(28)).append("',");
        sb.append("'").append(rs.getString(29)).append("',");
        sb.append("'").append(rs.getString(30)).append("',");
        sb.append("'").append(rs.getString(31)).append("',");
        sb.append("'").append(rs.getString(32)).append("',");
        sb.append("'").append(rs.getString(33)).append("',");
        sb.append("'").append(rs.getString(34)).append("',");
        sb.append("'").append(rs.getString(35)).append("'");
        sb.append(")");
        pw.println(sb.toString());
      }
    }catch (Exception e){
      e.printStackTrace();
    }finally{
      if( pw!=null){
        pw.flush();
        pw.close();
      }
    }
  }
}
